{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# getattr: 按名称访问对象的属性或方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<__main__.GfG object at 0x7e2b345f3340>\n",
      "<class '__main__.GfG'>\n",
      "<bound method GfG.call of <__main__.GfG object at 0x7e2b345f3340>>\n",
      "Vivek called with parameters 'arg'\n"
     ]
    }
   ],
   "source": [
    "class GfG:\n",
    "    \n",
    "    def __init__(self, name, age):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "\n",
    "    def call(self, x):\n",
    "        print(f\"{self.name} called with parameters '{x}'\")\n",
    "        return\n",
    "\n",
    "# initializing object\n",
    "obj = GfG(\"Vivek\", 10)\n",
    "print(obj)\n",
    "print(GfG)\n",
    "print(getattr(obj,'call'))\n",
    "\n",
    "getattr(obj,'call')('arg')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# hasattr: 检查对象是否具有给定的命名属性，如果存在则返回true，否则返回false"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "No Motto\n",
      "Time to execute hasattr : 0.00041961669921875\n",
      "No Motto\n",
      "Time to execute try : 7.224082946777344e-05\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "\n",
    "# declaring class\n",
    "class GfG:\n",
    "\tname = \"GeeksforGeeks\"\n",
    "\tage = 24\n",
    "\n",
    "# initializing object\n",
    "obj = GfG()\n",
    "\n",
    "# use of hasattr to check motto\n",
    "start_hasattr = time.time()\n",
    "if(hasattr(obj, 'motto')):\n",
    "\tprint(\"Motto is there\")\n",
    "else:\n",
    "\tprint(\"No Motto\")\n",
    "\n",
    "print(\"Time to execute hasattr : \" + str(time.time() - start_hasattr))\n",
    "\n",
    "# use of try/except to check motto\n",
    "start_try = time.time()\n",
    "try:\n",
    "\tprint(obj.motto)\n",
    "\tprint(\"Motto is there\")\n",
    "except AttributeError:\n",
    "\tprint(\"No Motto\")\n",
    "print(\"Time to execute try : \" + str(time.time() - start_try))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# setattr: 为对象属性分配其值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Before using setattr:\n",
      "\tname: None\n",
      "\tdescr: None\n",
      "After using setattr:\n",
      "\tname: GeeksForGeeks\n",
      "\tdescr: CS Portal\n",
      "\n",
      "New attribute created, gfg.value: 5\n"
     ]
    }
   ],
   "source": [
    "class Gfg:\n",
    "\tname = None\n",
    "\tdescr = None\n",
    "\n",
    "\n",
    "# initializing object\n",
    "gfg = Gfg()\n",
    "\n",
    "print(\"Before using setattr:\\n\"\n",
    "\tf\"\\tname: {gfg.name}\\n\"\n",
    "\tf\"\\tdescr: {gfg.descr}\")\n",
    "\n",
    "# setting value using setattr\n",
    "setattr(gfg, \"name\", \"GeeksForGeeks\")\n",
    "setattr(gfg, \"descr\", \"CS Portal\")\n",
    "\n",
    "print(\"After using setattr:\\n\"\n",
    "\tf\"\\tname: {gfg.name}\\n\"\n",
    "\tf\"\\tdescr: {gfg.descr}\")\n",
    "\n",
    "# creating new attribute using setattr\n",
    "setattr(gfg, 'value', 5)\n",
    "print(f\"\\nNew attribute created, gfg.value: {gfg.value}\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "jupter-env-10",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
